{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6900a38",
   "metadata": {},
   "source": [
    "# statsmodels包 测试"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94749c55",
   "metadata": {},
   "source": [
    "参考 https://www.cnblogs.com/-wenli/p/12868439.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "10ca69e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.api as sm\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a1556059",
   "metadata": {},
   "outputs": [],
   "source": [
    "nsample = 100\n",
    "x = np.linspace(0, 10, nsample)\n",
    "X = sm.add_constant(x)\n",
    "beta = np.array([1, 10])\n",
    "e = np.random.normal(size=nsample)\n",
    "y = np.dot(X, beta) + e"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e6ad8e3",
   "metadata": {},
   "source": [
    "OLS()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "942ee14a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<statsmodels.regression.linear_model.RegressionResultsWrapper at 0x22fa9536128>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = sm.OLS(y,X)\n",
    "results = model.fit()\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e187fa49",
   "metadata": {},
   "source": [
    "RegressionResults 对象 分解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d87d4419",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9989850933414269"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.rsquared"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "271f56f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6.33974764e-01, -9.41490671e-01,  5.71106533e-01,  1.95320697e+00,\n",
       "       -3.14214370e-01,  3.74703645e-01, -2.90565736e-01, -5.67305352e-01,\n",
       "       -1.47046493e-01,  1.49402665e+00, -8.00281651e-01, -2.24325764e+00,\n",
       "       -2.93879928e-01, -8.65655713e-01,  2.56225031e-01,  1.32358470e+00,\n",
       "       -4.08274198e-01, -5.82351919e-01, -8.38050538e-01, -5.21011356e-01,\n",
       "       -9.33083432e-01, -1.73960983e-01, -8.98211878e-01,  7.82298471e-01,\n",
       "        8.91022295e-01,  9.61644943e-02,  9.06489029e-01,  3.61901200e-01,\n",
       "        1.34489090e+00,  1.25979251e+00,  1.62081259e-01,  1.00157182e+00,\n",
       "        8.76677958e-01, -6.41323651e-01,  1.36888383e+00, -1.94581438e+00,\n",
       "        2.21088484e-01, -6.14246259e-01,  2.13805673e-01, -1.01914686e+00,\n",
       "        7.52932061e-01,  7.96428670e-01,  7.05433193e-01, -1.28511890e+00,\n",
       "        4.16063498e-02, -4.50858476e-01,  1.95846724e-01,  4.76811863e-01,\n",
       "        4.41690001e-01, -2.37914823e+00,  6.09443221e-01,  7.03804243e-01,\n",
       "       -1.28000441e+00,  2.22129451e-01,  1.64596459e+00,  3.64920289e-01,\n",
       "       -4.26452106e-01, -2.62532181e-01,  1.79319393e+00,  4.59868622e-02,\n",
       "       -4.37282389e-01, -4.11995700e-01, -1.86246543e+00,  6.48569050e-03,\n",
       "        8.37004336e-01,  8.68976679e-01,  9.00292948e-01, -1.46883893e-01,\n",
       "       -1.21890592e+00,  9.34473059e-02, -1.19415667e+00, -1.87053908e-01,\n",
       "        2.19216887e-01,  9.65868206e-01, -6.41644815e-01,  2.12784646e-01,\n",
       "        5.64936784e-01, -1.78715846e+00,  2.07060508e-01,  1.53565662e+00,\n",
       "        8.23411071e-01, -7.11703663e-01,  1.35801430e+00, -2.63466094e-01,\n",
       "       -7.52890917e-02, -9.05878697e-01,  8.15504817e-01, -7.93294350e-01,\n",
       "       -7.57649377e-01, -2.66108894e-01,  3.12748995e-01, -4.54473315e-01,\n",
       "       -3.16229269e-01, -4.06089547e-01, -1.94306026e+00,  7.47075824e-01,\n",
       "        1.41328753e+00, -1.20096183e+00,  1.59946343e+00,  2.06418222e-03])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.resid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75d55a72",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
